#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aac_one_frame_get.h"

#define READ_SIZE		(1024)
#define IN_FILENAME		"dec.aac"

aac_get_info s_aac_info;

int main(void)
{
	aac_one_frame_get_init(&s_aac_info);

	int ret = 0;
    FILE *in_file,*out_file;
	char frame_name[30] = "";
	int frame_num = 0;

    char *frame_buffer = NULL;
    frame_buffer = (char *)malloc(READ_SIZE);

	char * dst_video_buffer = NULL;
	dst_video_buffer = (char *)malloc(10000000);

	in_file = fopen(IN_FILENAME,"r");
    if(in_file == NULL)
    {
        printf("open in file error!\n");
    }
	int test = 0;
	int times = 20;

	while(1)
	{
		memset(frame_buffer,0,READ_SIZE);
		ret = fread(frame_buffer,READ_SIZE,1,in_file);
		if(ret != 1)
		{
			printf("ret = %d\n");
			printf("fread file error!\n");
		}
		int buffer_l = 0;

//		printf("xubinbin file:%s line:%d\n",__FILE__,__LINE__);
		int read_long = READ_SIZE;
		while(aac_one_frame_get_process(&s_aac_info, (char *) frame_buffer, read_long,dst_video_buffer, &buffer_l)!= 2)
		{
			if(test++ == times)
				break;
			if (buffer_l > 0)
			{
			//	printf("xubinbin file:%s line:%d\n",__FILE__,__LINE__);
				printf("buffer_size = *%d*\n",buffer_l);
				#if 1
				sprintf(frame_name,"%d.aac",++frame_num);
				out_file = fopen(frame_name,"w");
				if(out_file == NULL)
				{
					printf("open in file error!\n");
				}

				ret = fwrite(dst_video_buffer,buffer_l,1,out_file);
				if(ret != 1)
				{
					printf("ret = %d\n");
					printf("fwrite file error!\n");
				}
				fclose(out_file);
				#endif
			}
			buffer_l = 0;
			read_long = 0;
		}

		if(test >= times)
			break;

	}

    aac_one_frame_get_exit(&s_aac_info);
	free(frame_buffer);
	fclose(in_file);
    printf("Done!\n");
}


















